/* eslint-disable no-param-reassign */
/* eslint-disable max-len */
import map from 'lodash/map';
import isEmpty from 'lodash/isEmpty';
import JSONbig from 'json-bigint';
import { omit } from 'lodash';
import { TabkeyTypes } from '@/routes/app-pipeline/interface';
import { STAGE_CD, STAGE_CI } from '../stores/CONSTANTS';

function transformAdvancedSetting(data: any) {
  const result = {
    ...data,
    devopsCiPipelineFunctionDTOList: data?.devopsCiPipelineFunctionDTOList.filter((i: any) => i?.devopsPipelineId !== 0),
  };
  if (!result?.versionStrategy) {
    delete result.versionName;
  }
  return result;
}

function transformflowConfigurationCdHost(item:any) {
  if (item?.type === 'CD') {
    item.jobList?.forEach((jobItem: any) => {
      if (jobItem?.type !== 'cdHost') {
        return;
      }
      const metadataObj = JSONbig.parse(jobItem.metadata.replace(/'/g, '"'));
      if (!metadataObj.deployPayloadType) {
        metadataObj.hostId = metadataObj.hostId.id;
        if (metadataObj.hostConnectionVO) {
          metadataObj.hostConnectionVO.hostId = metadataObj.hostConnectionVO.hostId.id;
        }
      } else if (!jobItem.deployPayloadType && metadataObj.deployPayloadType) { // 修改时候没修改过
        if (metadataObj.hostId) {
          metadataObj.hostId = metadataObj.hostId.id;
        }
        if (metadataObj.hostGroupId) {
          metadataObj.hostGroupId = metadataObj.hostGroupId.id;
        }
      } else {
        const { deployPayloadType } = jobItem;
        if (deployPayloadType === 'host') {
          const hostId = jobItem.hostId?.id;
          metadataObj.hostId = hostId;
          metadataObj.hostGroupId = null;
          metadataObj.hostConnectionVO.hostId = metadataObj.hostConnectionVO.hostId.id;
        }
        if (deployPayloadType === 'group') {
          const hostGroupId = jobItem.hostGroupId?.id;
          metadataObj.hostId = null;
          metadataObj.hostGroupId = hostGroupId;
        }
      }
      omit(metadataObj, '__dirty');
      jobItem.metadata = JSONbig.stringify(metadataObj).replace(/"/g, "'");
    });
  }
}

export function handleTabDataTransform(tabsData:Record<TabkeyTypes, any>, transformSubmitBasic?: any) {
  const {
    basicInfo = {},
    flowConfiguration = [],
    ciConfigs = [],
    advancedSettings = {},
  } = tabsData;

  const {
    appService = {}, branch = [], pipelineName: name, objectVersionNumber,
  } = basicInfo;

  const finalData = {
    ...appService,
    ...transformAdvancedSetting(advancedSettings),
    name,
    ...transformSubmitBasic ? transformSubmitBasic(basicInfo) : {},
  };

  if (objectVersionNumber) {
    finalData.objectVersionNumber = objectVersionNumber;
  }

  if (branch && branch.length) {
    finalData.relatedBranches = map(branch, (item) => item?.branchName);
  }

  if (ciConfigs && ciConfigs.length) {
    finalData.devopsCiPipelineVariableDTOList = ciConfigs.filter((obj:any) => obj?.variableKey && obj?.variableValue && !isEmpty(obj));
  }

  if (flowConfiguration && flowConfiguration.length) {
    const devopsCiStageVOS: { type: string; }[] = [];
    const devopsCdStageVOS: { type: string; }[] = [];
    flowConfiguration.forEach((item: { type: string; }) => {
      transformflowConfigurationCdHost(item);
      if (item.type === STAGE_CI) devopsCiStageVOS.push(item);
      if (item.type === STAGE_CD) devopsCdStageVOS.push(item);
    });
    finalData.devopsCiStageVOS = devopsCiStageVOS;
    finalData.devopsCdStageVOS = devopsCdStageVOS;
  }

  return finalData;
}
